package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.PrinterTemplateService;
import com.junzhiit.nengji.cms.web.exception.PrinterTemplateNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.PrinterTemplateAddVO;
import com.junzhiit.nengji.cms.web.vo.PrinterTemplateEditVO;
import com.junzhiit.nengji.cms.web.vo.PrinterTemplateVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.PrinterTemplateDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.PrinterTemplate;
import com.junzhiit.nengji.model.Shop;

@Service
public class PrinterTemplateServiceImpl extends BaseServiceImpl implements PrinterTemplateService{
	
	@Autowired
	private PrinterTemplateDAO printerTemplateDAO;

	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Override
	public Map<String, Object> findPageAll(PrinterTemplateVO printerTemplateVO,DataTablesParamVO dtp){
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(printerTemplateVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		PrinterTemplate printerTemplate =new PrinterTemplate();
		BeanUtils.copyProperties(printerTemplateVO, printerTemplate);
		printerTemplate.setShopId(shop.getId());
		
		List<PrinterTemplate> list = printerTemplateDAO.findPageAll(printerTemplate,dtp.getStart(), dtp.getLength());
		int totalCount = printerTemplateDAO.getTotalCount(printerTemplate);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO findById(String id){
		ResultVO result=new ResultVO();
		
		PrinterTemplate data=printerTemplateDAO.query(id);
		if(data==null){
			throw new PrinterTemplateNotFoundException("小票模板不存在");
		}
		
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO add(PrinterTemplateAddVO printerTemplateAddVO){
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(printerTemplateAddVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		PrinterTemplate printerTemplate=new PrinterTemplate();
		BeanUtils.copyProperties(printerTemplateAddVO, printerTemplate);
		printerTemplate.setShopId(shop.getId());
		printerTemplate.setCode(printerTemplateAddVO.getCode().toString());
		
		printerTemplateDAO.insert(printerTemplate);
		return result;
	}
	
	@Override
	public ResultVO edit(String id,PrinterTemplateEditVO printerTemplateEditVO){
		ResultVO result=new ResultVO();
		
		PrinterTemplate data=printerTemplateDAO.query(id);
		if(data==null){
			throw new PrinterTemplateNotFoundException("小票模板不存在");
		}
		
		PrinterTemplate printerTemplate=new PrinterTemplate();
		BeanUtils.copyProperties(printerTemplateEditVO, printerTemplate);
		printerTemplate.setId(id);
		printerTemplate.setCode(printerTemplateEditVO.getCode().toString());
		
		boolean flag=printerTemplateDAO.update(printerTemplate);
		result.setData(flag);
		return result;
	}
	
	@Override
	public ResultVO getAll(PrinterTemplateVO printerTemplateVO){
		ResultVO result=new ResultVO();
		
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(printerTemplateVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		PrinterTemplate printerTemplate=new PrinterTemplate();
		BeanUtils.copyProperties(printerTemplateVO, printerTemplate);
		printerTemplate.setShopId(shop.getId());
		
		List<PrinterTemplate> data=printerTemplateDAO.getAll(printerTemplate);
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO getUseCount(String id,PrinterTemplateVO printerTemplateVO){
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(printerTemplateVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		PrinterTemplate printerTemplate=new PrinterTemplate();
		printerTemplate.setShopId(shop.getId());
		
		int count=printerTemplateDAO.getUseCount(id,printerTemplate);
		result.setData(count);
		return result;
	}
	
	@Override
	public ResultVO updateToDelete(String id){
		ResultVO result=new ResultVO();
		
		PrinterTemplate data=printerTemplateDAO.query(id);
		if(data==null){
			throw new PrinterTemplateNotFoundException("小票模板不存在");
		}
		boolean flag=printerTemplateDAO.updateToDelete(id);
		
		result.setData(flag);
		return result;
	}
	
}
